/**
* create by: w397090770
* Email:wyphao.2007@163.com
* create data: 2012.3.27
**/ 
#include <stdio.h>		//for printf,fgets... 
#include <stdlib.h>		//for exit
#include <vector>		//for vector
#include <string.h>		//for strlen
#include <string>		//for string
#include <algorithm>	//for sort

#define MAXLINE 256

using namespace std;

struct Number{
	int time;//���ʱ�� 
	int line;//������ 
};

static int LINE = 0;//��¼������ڵ��� 

int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime);
int strtoint(char *str);
int operator<(Number x,Number y);

//int main(int argc, char *argv[]){
//	char buf[MAXLINE];
//	
//	vector<string> sentences, finalSentence;
//	vector<Number> songTime;
//	FILE *fd;
//		
//	fd = fopen("����� - ����������.lrc", "r");
//	//fd = fopen("2.lrc", "r");
//	//fd = fopen("С���� - �ټ�.lrc", "r");
//	//fd = fopen("����¶ - ����Ĵ��.lrc", "r");
//	
//	if(fd == NULL){
//		perror("open file");
//		exit(1);
//	}
//	//������ 
//	while(fgets(buf, sizeof(buf), fd) != NULL){
//		LRCPrase(buf, sentences, songTime);
//		
//	}
//	sort(songTime.begin(), songTime.end());//����ʱ������ 
// 	//printf("%d\n", sentences.size());
//	/*vector<string>::iterator it = sentences.begin();
//	for(; it != sentences.end(); it++){
//		//printf("%d\t,%d\t", (*it).time, (*it).line);
//		printf("%s", (*it).c_str());
//	}*/
//	//��ʱ��˳�������� 
//	vector<Number>::iterator it1 = songTime.begin();
//	for(; it1 != songTime.end(); it1++){
//		//printf("%d\t,%d\n", (*it1).time, (*it1).line);
//		finalSentence.push_back(sentences[(*it1).line]);
//	}
//	
//	it1 = songTime.begin();
//	vector<string>::iterator it = finalSentence.begin();
//	for(; it1 != songTime.end() && it != finalSentence.end(); it1++, it++){
//		printf("%d\t,%d\t%s\n", (*it1).time, (*it1).line, (*it).c_str());
//	}
//	
//	return 0;
//}

int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime){
	if(strlen(str) == 1){//���� 
		return 0;
	}else{
		char *p, *q, *temp;
		q = str;
		//����ʱ��� 
		while((p = strchr(q, '[')) != NULL && (temp = strchr(q, ']')) != NULL){
			q = p + 1;
			q[temp - q] = '\0';
			//printf("%s\t%d\n", q);
			
			struct Number number;
			if((number.time = strtoint(q)) < 0){
				return 0;
			}
			number.line = LINE;
			songTime.push_back(number);
			q = temp + 1;
		}
		//printf("%s", temp + 1);
		//��ȡ��� 
		p = ++temp;
		while(*temp){
			temp++;
		}
		p[temp - p] = '\0';
		//printf("%s", p);		
		string s(p);
		if(s.find("\n", 0) != -1)
			s.replace(s.find("\n", 0), 1, "");
		sentences.push_back(s);
		LINE++;
		return 1;
	}
	
}
//��charת��Ϊint 
int chartoint(char ch){
	return ch - '0';
}

int strtoint(char *str){//����ʱ�䣬΢�� 
	if(isdigit(str[0]) && isdigit(str[1])
		&& isdigit(str[0]) && isdigit(str[0])
		&& isdigit(str[0]) && isdigit(str[0])){
			int mintue = chartoint(str[0]) * 10 + chartoint(str[1]);
			int second = chartoint(str[3]) * 10 + chartoint(str[4]);
			int microsecond = chartoint(str[6]) * 10 + chartoint(str[7]);
			return (mintue * 60 + second) * 1000 + microsecond * 10;
		}
	return -1;
}
//����<����������sort����Ƚ��� 
int operator<(Number x,Number y){
	 return x.time < y.time;
}
